/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.supinfo.youfood.web.servlet.admin.employe;

import com.supinfo.youfood.entity.Employe;
import com.supinfo.youfood.entity.TableResto;
import com.supinfo.youfood.service.EmployeService;
import com.supinfo.youfood.service.RestaurantService;
import com.supinfo.youfood.service.TableRestoService;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author TaKeO
 */
@WebServlet(name = "AssigneTableToEMployeServlet", urlPatterns = {"/admin/assigne-table"})
public class AssigneTableToEMployeServlet extends HttpServlet
{
    @EJB
    private EmployeService employeService;
    
    @EJB
    private RestaurantService restoService;
    
    @EJB
    private TableRestoService tableService;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        List<Employe> employes;
        Employe employe = (Employe) request.getSession().getAttribute("currentEmp");
        if(employe.getType_employe().getDroits() < 700)
        {
            employes = restoService.findRestoById(employe.getResto().getId_restaurant()).getEmployes();
        }
        else
        {
            employes = employeService.getAllEmploye();
        }
        List<Employe> employesToRemove = new ArrayList();
        for (int i = 0; i < employes.size(); i++)
        {
            if(employes.get(i).getType_employe().getDroits() != 222)
            {
                employesToRemove.add(employes.get(i));
            }
        } 
        employes.removeAll(employesToRemove);
        request.setAttribute("employes", employes);
        
        request.getRequestDispatcher("/jsp/admin/assigne-table.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        Employe employe = (Employe) request.getSession().getAttribute("currentEmp");
        List<Employe> employes;
        if(employe.getType_employe().getDroits() < 700)
        {
            employes = restoService.findRestoById(employe.getResto().getId_restaurant()).getEmployes();
        }
        else
        {
            employes = employeService.getAllEmploye();
        }
        List<Employe> employesToRemove = new ArrayList();
        for (int i = 0; i < employes.size(); i++)
        {
            if(employes.get(i).getType_employe().getDroits() != 222)
            {
                employesToRemove.add(employes.get(i));
            }
        }
        employes.removeAll(employesToRemove);
        request.setAttribute("employes", employes);
        
        if(request.getParameter("employe") != null)
        {
            Employe emp = employeService.findEmployeById(new Long(request.getParameter("employe"))); 
            List<TableResto> tablesEmp = emp.getTables();
            request.setAttribute("tables", tablesEmp);
            
            List<TableResto> tablesRestos = emp.getResto().getTables();
            tablesRestos.removeAll(tablesEmp);
            request.setAttribute("tablesRestos", tablesRestos);

            request.getRequestDispatcher("/jsp/admin/assigne-table.jsp").forward(request, response);
        }
        else
        {
            List<TableResto> tables = new ArrayList();
            String select[] = request.getParameterValues("id");
            if (select != null && select.length != 0)
            {
                for (int i = 0; i < select.length; i++)
                {
                    TableResto table = tableService.findTableBy(new Long(select[i]));
                    tables.add(table);
                    table.setServeur(employe);
                    tableService.updateTableResto(table);
                }
            }

            employe.setTables(tables);
            employeService.refreshEmploye(employe);
            response.sendRedirect("/YouFood/admin/assigne-table");
        }
    }

}
